package com.imooc.Dao;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.imooc.beans.Message;
import com.imooc.db.DBAccess;

public class MessageDao {
	
	
	public List<Message> queryMessageList(String command ,String description){
		DBAccess dbAccess = new DBAccess();
		SqlSession sqlSession = null;
		List<Message> messageList = new ArrayList<Message>();
		try {
			sqlSession = dbAccess.getSqlSession();
			Message message =new Message();
			message.setCommand(command);
			message.setDescription(description);
			//通过 sqlSession 执行 SQL语句
			//messageList= sqlSession.selectList("com.imooc.dao.MessageMapper.queryMessageList",message);
			IMessage imessage = sqlSession.getMapper(IMessage.class);
			messageList = imessage.queryMessageList(message);
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if (sqlSession!=null) {
				sqlSession.close();
			}
		}
		
		return messageList;
	}
	
	/**
	 * 单条删除
	 * @param id
	 */
	public void deleteOne(int id){
		DBAccess dbAccess = new DBAccess();
		SqlSession sqlSession = null;
		try {
			sqlSession = dbAccess.getSqlSession();
			sqlSession.delete("com.imooc.dao.MessageMapper.deleteOne",id);
			//通过 sqlSession 执行 SQL语句
			//messageList= sqlSession.selectList("com.imooc.dao.MessageMapper.queryMessageList",message);
			sqlSession.commit();
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			if (sqlSession!=null) {
				sqlSession.close();
			}
		}
		
	}
	
	public List<Message> queryMessageListByPage(Map<String,Object> map){
		List<Message> listMessage=new ArrayList();
		
		return listMessage;		
	}
	
	/**
	 * 根据查询条件查询消息列表
	 * @param command
	 * @param description
	 * @return
	 */
//	public List<Message> queryMessageList(String command ,String description){
//		List<Message> messageList =new ArrayList<Message>();
//		try {
//			Class.forName("com.mysql.jdbc.Driver");
//			Connection conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456");
//			StringBuilder sql =new StringBuilder("select ID, COMMAND, DESCRIPTION, CONTENT from message where 1=1 ");
//			List<String> paramList =new ArrayList<String>();
//			if (command!=null&&!"".equals(command.trim())) {
//				sql.append(" and COMMAND=? ");
//				paramList.add(command);
//			}
//			// mysql 的字符串拼接用空格 拼接
//			if (description!=null&&!"".equals(description.trim())) {
//				sql.append(" and DESCRIPTION like '%' ? '%' ");
//				paramList.add(description);
//			}
//			PreparedStatement statement= conn.prepareStatement( sql.toString());
//			ResultSet rs = statement.executeQuery();
//			while (rs.next()) {
//				Message message = new Message();
//				messageList.add(message);
//				message.setId(Integer.parseInt(rs.getString("ID")));
//				message.setCommand(rs.getString("COMMAND"));
//				message.setDescription(rs.getString("DESCRIPTION"));
//				message.setContent(rs.getString("CONTENT"));
//			}
//			
//		} catch (ClassNotFoundException e) {
//			e.printStackTrace();
//		} catch (SQLException e) {
//			e.printStackTrace();
//		}
//		
//		return messageList;
//	}
	
}
